﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Kalkulator
{
    public partial class Form1 : Form
    {

        double variable;
        double m=0;

        char sign;
        int spr = 0;
        

        public Form1()
        {
            InitializeComponent();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void licz1_Click(object sender, EventArgs e)
        {
           
                textBox1.AppendText("1");
                
            
          
            
        }

        private void licz2_Click(object sender, EventArgs e)
        {
            textBox1.AppendText("2");
        }

        private void licz3_Click(object sender, EventArgs e)
        {
            textBox1.AppendText("3");
        }

        private void licz4_Click(object sender, EventArgs e)
        {
            textBox1.AppendText("4");
        }

        private void licz5_Click(object sender, EventArgs e)
        {
            textBox1.AppendText("5");
        }

        private void licz6_Click(object sender, EventArgs e)
        {
            textBox1.AppendText("6");
        }

        private void licz7_Click(object sender, EventArgs e)
        {
            textBox1.AppendText("7");
        }

        private void licz8_Click(object sender, EventArgs e)
        {
            textBox1.AppendText("8");
        }

        private void licz9_Click(object sender, EventArgs e)
        {
            textBox1.AppendText("9");
        }

        private void licz0_Click(object sender, EventArgs e)
        {
            textBox1.AppendText("0");
        }

        private void przecinek_Click(object sender, EventArgs e)
        {
            textBox1.AppendText(",");
        }

        private void kasuj_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            variable = 0;
        }

       public void dodawanie_Click(object sender, EventArgs e)
        {
            
            //dodawanie.Visible = false;

          

            variable = double.Parse(textBox1.Text);
            
            sign = '+';

            textBox1.Text = "";          
            
        }

       private void odejmowanie_Click(object sender, EventArgs e)
       {
           variable = double.Parse(textBox1.Text);

           sign = '-';

           textBox1.Text = "";
       }

       private void mnozenie_Click(object sender, EventArgs e)
       {
           variable = double.Parse(textBox1.Text);

           sign = '*';

           textBox1.Text = "";
       }

       private void dzielenie_Click(object sender, EventArgs e)
       {
           variable = double.Parse(textBox1.Text);

           sign = '/';

           textBox1.Text = "";
       }


        private void dodaj_Click(object sender, EventArgs e)
        {
            switch (sign)
            {

                case '+':

                 

                        textBox1.Text = (variable + double.Parse(textBox1.Text)).ToString();
                        //variable = 0;

                    break;


                case '-':

                    textBox1.Text = (variable - double.Parse(textBox1.Text)).ToString();

                    break;

                case '/':

                    if (textBox1.TextLength == 1 && textBox1.Text[0].Equals('0'))
                    {

                        textBox1.Text = "Error! divide by zero!";

                    }

                    else
                    {

                        textBox1.Text = ((double)variable / double.Parse(textBox1.Text)).ToString();

                    }

                    break;

                case '*':

                    textBox1.Text = (variable * double.Parse(textBox1.Text)).ToString();

                    break;
                         

            }
        }

        private void znak_Click(object sender, EventArgs e)
        {
            int dlugosc = textBox1.Text.Length;

            if (dlugosc > 0)
            {
                if ((textBox1.Text[0]).Equals('-'))
                {
                    textBox1.Text = textBox1.Text.Substring(1, dlugosc - 1);
                }
                else textBox1.Text = "-" + textBox1.Text;
            }
            else textBox1.Text = "-" + textBox1.Text;
        }

        private void backspace_Click(object sender, EventArgs e)
        {
            int dlugosc = textBox1.Text.Length;

            if (dlugosc > 0)
            {
                textBox1.Text = textBox1.Text.Substring(0, dlugosc - 1);

            }
        }

        private void pamiec_Click(object sender, EventArgs e)
        {
            m = double.Parse(textBox1.Text);
        }

        private void przywroc_pamiec_Click(object sender, EventArgs e)
        {
            textBox1.Text = (m).ToString();
        }

        private void kasowanie_pamiec_Click(object sender, EventArgs e)
        {
            m = 0;
        }

        private void pamiec_dodawanie_Click(object sender, EventArgs e)
        {
            textBox1.Text = (m + double.Parse(textBox1.Text)).ToString();
        }

        private void pamiec_kasowanie_Click(object sender, EventArgs e)
        {

        }

        

        
        

     

      
    }
}
